package com.frontop.ao.gateway.config;

import cn.hutool.core.util.ArrayUtil;
import com.frontop.ao.gateway.component.RestAuthenticationEntryPoint;
import com.frontop.ao.gateway.component.RestfulAccessDeniedHandler;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
/**
 * Gateway用的是webflux，不同于Servlet
 */
@AllArgsConstructor
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
    private final IgnoreUrlsConfig ignoreUrlsConfig;
    private final RestfulAccessDeniedHandler restfulAccessDeniedHandler;
    private final RestAuthenticationEntryPoint restAuthenticationEntryPoint;
   // private final AuthorizationManager authorizationManager;
    /**
     * 主要过滤配置类 开放所有请求  禁用csrf保护
     * @param http
     * @return
     */
    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        //自定义处理JWT请求头过期或签名错误的结果
        http.exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler)//处理未授权
                .authenticationEntryPoint(restAuthenticationEntryPoint);//处理未认证
        //ArrayUtil.toArray(ignoreUrlsConfig.getUrls(),String.class)
        //配置权限路径  关闭CSRF保护
        http.authorizeExchange()
                .pathMatchers("/**").permitAll()//白名单配置
                .anyExchange().authenticated()
                .and()
                .httpBasic().and()
                .formLogin()
                .and().csrf().disable();
        return http.build();
    }

}
